package br.ucb.GtTunningLog.client;

import java.util.Vector;

import br.ucb.GtTunningLog.client.BlogEntry.MyHandler;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dev.shell.GridPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.DockPanel.DockLayoutConstant;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GtTunningLog implements EntryPoint {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String			SERVER_ERROR	= "An error occurred while "
																+ "attempting to contact the server. Please check your network "
																+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private GreetingServiceAsync	greetingService	= GWT
																.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		final String pageHome = "GtHome";
		final String pageCarros = "GtCarros";
		final String pageFotos = "GtFotos";
		final String pagePistas = "GtPistas";
		
		String bodyName = RootPanel.getBodyElement().getId();
		
		if(bodyName.equals(pageCarros)){
			RootPanel.get("content").add(getCarBody());
		}else if(bodyName.equals(pagePistas)){
			RootPanel.get("content").add(getTrackBody());
		}else if(bodyName.equals(pageFotos)){
			RootPanel.get("content").add(getPhotosBody());
		}else if(bodyName.equals("blogentry")){
			//RootPanel.get("content").add(BlogEntry.createNewBlogPage(this.greetingService));
			//RootPanel.get("content").add(BlogEntry.createNewBlogPage());
			RootPanel.get("content").add(getBlogEntryPage());
		}else{				
			RootPanel.get("content").add(getHomeBody());
			
			testeCarregarBlog();
		}
		
//		Panel panel = new FlowPanel();panel.''
		
//		RootPanel.get("content").
				
	}
	
	private Widget getBlogEntryPage(){
		 Button saveEntryButton;
		 Button cancelButton;
		 Button viewPost;
		 final TextArea initBlogText;
		 final TextArea fullBlogText;
		 FlowPanel panel;
		 String pageHead;
		 DialogBox dialogBox;
		 HTML serverResponseLabel;
		
			panel = new FlowPanel();
			//initLabel = new Label("Descreva aqui as informações gerais do post");
			pageHead = "Descreva aqui o seu post: ";
			saveEntryButton = new Button("Salvar Post");
			cancelButton = new Button("Cancelar Post");
			viewPost = new Button("Visualizar Post");
			initBlogText = new TextArea();
			fullBlogText = new TextArea();
			serverResponseLabel = new HTML("");
			
			dialogBox = new DialogBox();
			dialogBox.setText("Remote Procedure Call");
			dialogBox.setAnimationEnabled(true);
			
			//Configura o painel
			panel.setWidth("600px");
			
			//configura os campos
			initBlogText.setWidth("550px");
			initBlogText.setHeight("200px");
			
			fullBlogText.setWidth("550px");
			fullBlogText.setHeight("200px");
			
			//adiciona os elmentos ao painel
			String htmlHomeBody ="<div class=\"post\"><h1 class=\"title\"><a href=\"#\">" + pageHead +
				"</a></h1><br>&nbsp;<br>";
			panel.add(new HTML(htmlHomeBody));
//			panel.add(initLabel);
			panel.add(initBlogText);
			panel.add(new HTML("<br>&nbsp<br>"));
			panel.add(new HTML("<div class=\"post\"><h1 class=\"title\">" +
					"<a href=\"#\">Informe o conteúdo do seu post" +
					"</a></h1><br>&nbsp;<br>"));
			panel.add(fullBlogText);
			panel.add(new HTML("<br>&nbsp;<br>"));
			
			panel.add(saveEntryButton);
			//panel.add(new HTML("&nbsp;"));
			panel.add(cancelButton);
			panel.add(viewPost);
			panel.add(new HTML("</div>"));
			
			// Add a handler to close the DialogBox
			viewPost.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					RootPanel.get("post").add(new Post("Flebinha Testando", 
							initBlogText.getText(), "O Mestre", fullBlogText.getText()));
				}
			});
			
			// Create a handler for the sendButton and nameField
			class MyHandler implements ClickHandler, KeyUpHandler {

				/**
				 * Fired when the user clicks on the sendButton.
				 */
				public void onClick(ClickEvent event) {
					//RootPanel.get("content").add(new HTML("OI FUNFOU!!!"));
					savePost();
					//RootPanel.get("content").add(new HTML("OI FUNFOU222!!!"));
					
				}

				/**
				 * Fired when the user types in the nameField.
				 */
				public void onKeyUp(KeyUpEvent event) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
						savePost();
					}
				}

				/**
				 * Send the name from the nameField to the server and wait for a response.
				 */
				private void savePost() {
					Post post = new Post("Flebinha Testando", 
							initBlogText.getText(), "O Mestre", fullBlogText.getText());
					String strPost = post.getString();
					greetingService.addPost(strPost,
							new AsyncCallback<String>() {

								public void onFailure(Throwable caught) {
									// Show the RPC error message to the user
//									dialogBox
//											.setText("Remote Procedure Call - Failure");
//									serverResponseLabel
//											.addStyleName("serverResponseLabelError");
									//serverResponseLabel.setHTML(SERVER_ERROR);
									RootPanel.get("content").add(new HTML("Falhou"));
									//dialogBox.center();
									//closeButton.setFocus(true);
								}

								public void onSuccess(String result) {
									//RootPanel.get("content").add(new HTML("OI FUNFOU!!!"));
									RootPanel.get("content").add(new HTML("<br><br><div " +
											"class=\"post\"><h1 " +
											"class=\"title\"><a href=\"#\"> post adicionado" +
											"</a></h1><br>&nbsp;<br>"));
									
//									dialogBox.center();
									//closeButton.setFocus(true);
								}
							});
				}
			}
			
			MyHandler myHandler = new MyHandler();	
			saveEntryButton.addClickHandler(myHandler);
		return panel;
	}
	
	private Widget getHomeBody(){
		String htmlHomeBody = "<div class=\"post\">" 
			+ "<h1 class=\"title\"><a href=\"#\">Bem vindo ao Site GtTunningLog </a></h1>"
			+ "<p class=\"meta\">Postado por <a href=\"#\">Eduardo</a>em 28 de Maio de 2008"
			+ "&nbsp;&bull;&nbsp; <a href=\"#\" class=\"comments\">Comments (64)</a> &nbsp;&bull;&nbsp; <a href=\"#\" class=\"permalink\">Artigo Completo</a></p>"
			+ "<div class=\"entry\">"
			+ "<p>Esse é o site <strong>GTTunningLog</strong>, um site gratuito para a comunidade de jogadores de <strong>Gran Turismo 5</strong>, que permite registrar todos os logs de configurações de carros e pistas existentes no jogo. Com esse site esperamos aumentar a rede de conhecimento sobre configurações ou simplesmente trocar figurinhas!;-)</p>"
			+ "<p>O fenômeno das pistas está de volta. Depois de um tenebroso inverno que o afastou, Lexus_Hanah volta ao cenário marcando os melhores tempos da temporada.</p>"
			+ "</div>"
			+ "</div>";
		
		
		return new HTML(htmlHomeBody);
	}
	
	private HTML testeCarregarBlog(){
		greetingService.getPosts(
				new AsyncCallback<String>() {

			public void onFailure(Throwable caught) {
				RootPanel.get("content").add(new HTML("teste asda"));
			}

			public void onSuccess(String stringVector) {
				RootPanel.get("content").add(new HTML(stringVector));
			}
		});
		return new HTML("Afferson!!!");
	}
	
	private Widget getCarBody(){
		//Adicionar a tab panel
		TabPanel tab = new TabPanel();
		FlowPanel flow = new FlowPanel();
		
		//Configura o tab
		tab.setWidth("600px");
		
		//Configura o flow
		//flow.setWidth("400px");
		
		//Tab Carro
		Label nameLabel = new Label("Nome do Carro");
		TextBox nameField= new TextBox();
		flow.add(nameLabel);
		flow.add(nameField);
		Label modelLabel = new Label("Modelo do Carro: ");
		TextBox modelField = new TextBox();
		flow.add(modelLabel);
		flow.add(modelField);
		Label yearLabel = new Label("Ano do Carro: ");
		TextBox yearField = new TextBox();
		flow.add(yearLabel);
		flow.add(yearField);
		Label pointLabel = new Label("Pontuação do Carro: ");
		TextBox pointField = new TextBox();
		flow.add(pointLabel);
		flow.add(pointField);
		
		tab.add(flow, "Carro");
		
		//Tab Configurações
		flow = new FlowPanel();
		nameLabel = new Label("Potênia(cv)");
		nameField = new TextBox();
		flow.add(nameLabel);
		flow.add(nameField);
		tab.add(flow, "Configurações");
		
		//Tab Imagens
		flow = new FlowPanel();
		HTML htmlImages = new HTML("<table border=\"1\">" +
				"<tr>" +
					"<td>" +
					"Tem que por uma imagem aqui!!" +
					"</td>" +
				"</table>");
		nameLabel = new Label("Potência(cv)");
		nameField = new TextBox();
		flow.add(htmlImages);
		tab.add(flow, "Configurações");
		
		//Final do config de carros
		tab.selectTab(0);
		
		//retorna a tab
		return tab;
		
		
	}

	private Widget getPhotosBody(){
		return new HTML("");
	}
	
	private Widget getTrackBody(){
		return new HTML("");
	}
}
